﻿<?php
class PortletCode extends CCodeModel
{
	public $className;
	public $baseClass='CPortlet';
	public $scriptPath='application.components';

	public function rules()
	{
		return array_merge(parent::rules(), array(
					array('className,baseClass,scriptPath', 'required'),
					array('className', 'match', 'pattern'=>'/^\w+$/'),
					array('baseClass', 'match', 'pattern'=>'/^\w+$/', 'message'=>'{attribute} should only contain word characters.'),
					array('baseClass', 'sticky'),
					array('scriptPath', 'validateScriptPath'),
					array('scriptPath', 'sticky'),
					));
	}

	public function attributeLabels()
	{
		return array_merge(parent::attributeLabels(), array(
					'baseClass'=>'Base Class',
					'className'=>'Portlet Class Name',
					'scriptPath'=>'Script Path',
					));
	}

	public function validateScriptPath($attribute,$params)
	{
		if($this->hasErrors('scriptPath'))
			return;
		if(Yii::getPathOfAlias($this->scriptPath)===false)
			$this->addError('scriptPath','Script Path must be a valid path alias.');
	}

	public function prepare()
	{
		$path=Yii::getPathOfAlias($this->scriptPath).'/' . ucfirst($this->className) . '.php';
		$code=$this->render($this->templatepath.'/portlet.php');
		$this->files[]=new CCodeFile($path, $code);
	}

	public function successMessage()
	{
		$output='<p>The Portlet has been generated successfully.</p>';
		$code=$this->render($this->templatePath.'/portlet.php');
		return $output.highlight_string($code,true);
	}

	public function p($name)
	{
		return Yii::app()->params[$name];
	}
}
?>
